<?php
/**
* @version		$Id: stavke_ponudes.php 3 2011-10-28 15:49:24Z netix.mail@gmail.com $
* @package		ALM Hotel Menager	
* @subpackage	com_almhm
* @author		ALM Studio
* @copyright	Copyright 2011 ALM Studio. All rights reserved
* @licence		Commercial
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHmModelStavke_ponudes extends JModelList
{
	
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'StavkaId', 'a.StavkaId',
				'StavkaPonuda', 'a.StavkaPonuda',
				'StavkaNaziv', 'a.StavkaNaziv',
				'StavkaSlika', 'a.StavkaSlika',		
				'StavkaOpis', 'a.StavkaOpis',
				'StavkaCijenaOpis', 'a.StavkaCijenaOpis',
				'StavkaCijena', 'a.StavkaCijena',
				'StavkaDostupnost', 'a.StavkaDostupnost',
				'StavkaStatusOdDatuma', 'a.StavkaStatusOdDatuma',
				'StavkaStatusDoDatuma', 'a.StavkaStatusDoDatuma',
				'StavkaStatus', 'a.StavkaStatus',
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}
		
		$ponudaId = $app->getUserStateFromRequest($this->context.'.filter.ponuda_id', 'filter_ponuda_id');
		$this->setState('filter.ponuda_id', $ponudaId);

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
	
		
		parent::populateState('a.StavkaId', 'desc');		
				
	}
	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);

		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almhm_ponuda_stavke AS a');
		
		$ponudaId = $this->getState('filter.ponuda_id');
		if (is_numeric($ponudaId)) {
			$query->where('a.StavkaPonuda = '.$ponudaId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'StavkaNaziv:') === 0) {
				$query->where('a.StavkaNaziv = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.StavkaNaziv LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.StavkaId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	public function getPonuda() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('p.PonudaId AS value, p.PonudaNaziv AS text');
		$query->from('#__almhm_ponude AS p');
		$query->join('INNER', '#__almhm_ponuda_stavke AS t ON t.StavkaPonuda = p.PonudaId');
		$query->group('p.PonudaId');
		$query->order('p.PonudaNaziv');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
}



